package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.EmployeeBean;
import com.bean.LeaveDetailsBean;
import com.exception.LMSException;
import com.interfaces.EmployeeManager;
import com.interfaces.LeaveManager;
import com.manager.EmployeeManagerImpl;
import com.manager.LeaveManagerImpl;

/**
 * Servlet implementation class LeaveReportServlet
 */
public class LeaveReportServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LeaveReportServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final String action = request.getParameter("MODO");
		final EmployeeManager employeeManager = new EmployeeManagerImpl();
		LeaveManager leaveManager = new LeaveManagerImpl();
		System.out.println(action);
		if (action.equals("default")) {
			final String userId = (String) request.getSession().getAttribute("userid");
			System.out.println(userId);
			try {
				List<EmployeeBean> employeeList = employeeManager.getEmployeeList (userId);
				for(EmployeeBean bean :employeeList){
					System.out.println(bean.toString());
				}
				request.setAttribute("empList",employeeList );
			} catch (LMSException e) {
				e.printStackTrace();
				request.setAttribute("error", e.getMessage());
			}
		}
		if (action.equals("formSubmit")) {
			final String userId = (String) request.getSession().getAttribute("userid");
			System.out.println(userId+"adai");
			final String empId = request.getParameter("employee");
			final String leaveType = request.getParameter("leaveType");
			System.out.println(empId + leaveType);
			try {
				EmployeeBean employee = employeeManager.getEmployeeBean(empId);
				List<LeaveDetailsBean> leaveList = leaveManager.getLeaveList(empId, leaveType);
				List<EmployeeBean> employeeList = employeeManager.getEmployeeList (userId);
				request.setAttribute("empList",employeeList );
				request.setAttribute("leaveList",leaveList );
				request.setAttribute("empName",employee.getEmpName() );
				if(leaveList.isEmpty()){
					request.setAttribute("Message"," No Record Found !" );
				}
			} catch (LMSException e) {
				e.printStackTrace();
				request.setAttribute("error", e.getMessage());
			}
		}
		final RequestDispatcher reqDispatch = getServletContext()
				.getRequestDispatcher("/LeaveReport.jsp");
		reqDispatch.forward(request, response);
	}

}
